<cfcomponent extends="assets.calgrants.com.Base">

	<cfscript>
        import assets.calgrants.com.service.EvaluationService;
		this.service = new EvaluationService();
	</cfscript>

	<cffunction name="get" output="false" access="remote" returntype="assets.calgrants.com.bo.Evaluation">
		<cfreturn this.service.get(argumentCollection = arguments) />
	</cffunction>
	
	<cffunction name="query" output="false" access="public" returntype="query">
		<cfreturn this.service.read(argumentCollection = arguments) />
	</cffunction>
	
	<cffunction name="save" output="false" access="public" returntype="any">
        <cfscript>
			var evaluation = "";
			var evaluation_ = this.service.get(Evaluation_ID = arguments.Evaluation_ID);
			if (IsInstanceOf(arguments[1], "assets.calgrants.com.bo.Evaluation")) {
				evaluation = arguments[1];
			} else {
				var args = {};
				StructAppend(args, arguments);
				args.obj = Duplicate(evaluation_);
				evaluation = this.service.load(argumentCollection = args);
			}
		    var validator = getValidator();
		    var validationResults = validator.validate(evaluation);
		    if (validationResults.getIsSuccess()) {
                evaluation.setEvaluation_ID(this.service.save(evaluation));
                return evaluation;
            } else {
                return validationResults.getFailures();
            }
        </cfscript>
	</cffunction>
	
	<cffunction name="delete" output="false" access="public" returntype="boolean">
		<cfreturn this.service.destroy(argumentCollection = arguments) />
	</cffunction>
	<!--- report --->
	<cffunction name="report" output="false" access="public" returntype="array">
		<cfargument name="programYears" required="false" default="" />
		<cfargument name="evaluationIDs" required="false" default="" />
		<cfargument name="studentIDs" required="false" default="" />
		<cfargument name="workshopIDs" required="false" default="" />
		<cfargument name="siteIDs" required="false" default="" />
		<cfargument name="organizerIDs" required="false" default="" />
		<cfargument name="managerIDs" required="false" default="" />
		<cfargument name="organizationIDs" required="false" default="" />
		<cfargument name="countyIDs" required="false" default="" />
		<cfargument name="regionIDs" required="false" default="" />
		<cfargument name="excludeOtherResponses" type="boolean" required="false" default="true" />
		<cfscript>
			var evaluations = this.service.read(programYears = arguments.programYears, workshopIDs = arguments.workshopIDs, organizationIDs = arguments.organizationIDs, regionIDs = arguments.regionIDs);
			var surveyGateway = new assets.calgrants.com.gateway.SurveyGateway();
			var survey = surveyGateway.get(Program_Year = arguments.programYears);
			var surveyMap = ArrayNew(1);
			var questionNumber = "";
			var answerNumber = "";
			var response = "";
			var gradDateRef = "";
			var dob = "";
			var dob17 = "";
			var dob19 = "";
			var isSenior = "";
		</cfscript>
		<!--- 
		Create survey value-to-answer map.  Map is an array of questions which contains
		a struct of answers.  Answers are formed as answer_value = answer_number, so..
		
		array[1]["1"] = "1"
		array[1]["2"] = "2"
		array[1]["5"] = "3"
		array[1]["4"] = "4"
		
		Using the map we can get the answer_number from the answer_value by array[i][value]
		so that array[1]["5"] will return "3" corresponding to question 3.
		--->
		<!--- generate survey map --->
		<cfloop index="questionNumber" from="1" to="#ArrayLen(survey)#">
			<cfif survey[questionNumber].Question_Active eq 1>
				<cfset surveyMap[questionNumber] = StructNew() />
				<cfset surveyMap[questionNumber].questionField = survey[questionNumber].Question_Field />
				<cfset surveyMap[questionNumber].questionType = survey[questionNumber].Question_Type />
				<cfset surveyMap[questionNumber].question = survey[questionNumber].Question />
				<cfloop index="answerNumber" from="1" to="#ArrayLen(survey[questionNumber].Answers)#">
					<cfif surveyMap[questionNumber].questionType neq "Text entry">
						<cfset surveyMap[questionNumber]["#survey[questionNumber].Answers[answerNumber].Answer_Value#"] = survey[questionNumber].Answers[answerNumber].Answer_Number />
					</cfif>
				</cfloop>
			</cfif>
		</cfloop>
		<!--- tally response data --->
		<cfloop query="evaluations">
			<!--- is senior? --->
			<cfif Len(Trim(Program_Year)) gt 0>
				<cfset gradDateRef = CreateDate(ListLast(Program_Year, "-"), 8, 18) />
			</cfif>
			<cfif IsDate(DOB)>
				<cfset dob = DOB />
			</cfif>
			<cfset dob17 = DateAdd("yyyy", -17, gradDateRef) />
			<cfset dob19 = DateAdd("yyyy", -19, gradDateRef) />
			<cfset isSenior = true>
			<cfif q7 neq "1" and (
				NOT IsDate(dob) or 
				(DateFormat(dob, "yyyymmdd") gte DateFormat(dob17, "yyyymmdd") or DateFormat(dob, "yyyymmdd") lte DateFormat(dob19, "yyyymmdd"))
				)>
				<cfset isSenior = false />
			</cfif>
			<cfloop index="questionNumber" from="1" to="#ArrayLen(surveyMap)#">
				<cfset response = evaluations[surveyMap[questionNumber].questionField][CurrentRow] />
				<!--- responded --->
				<cfif Len(Trim(response)) gt 0>
					<cfset survey[questionNumber].Response_Count = IncrementValue(survey[questionNumber].Response_Count) />
					<cfif isSenior>
						<cfset survey[questionNumber].Senior_Response_Count = IncrementValue(survey[questionNumber].Senior_Response_Count) />
					</cfif>
					<!--- radios and checkboxes --->
					<cfif surveyMap[questionNumber].questionType neq "Text entry">
						<cfif ListLen(response) gt 1>
							<cfset survey[questionNumber].Multi_Response_Count = IncrementValue(survey[questionNumber].Multi_Response_Count) />
							<cfif isSenior>
								<cfset survey[questionNumber].Senior_Multi_Response_Count = IncrementValue(survey[questionNumber].Senior_Multi_Response_Count) />
							</cfif>
						</cfif>
						<cfloop index="responseValue" list="#response#">
							<cfset answerNumber = surveyMap[questionNumber]["#responseValue#"] />
							<cfset survey[questionNumber].Answers[answerNumber].Response_Count = IncrementValue(survey[questionNumber].Answers[answerNumber].Response_Count) />
							<cfif survey[questionNumber].Answers[answerNumber].Answer_Type eq "Radio other" or survey[questionNumber].Answers[answerNumber].Answer_Type eq "Checkbox other">
								<cfif Len(Trim(evaluations["q" & questionNumber & "_other"][CurrentRow])) gt 0 and NOT arguments.excludeOtherResponses>
									<cfset ArrayAppend(survey[questionNumber].Answers[answerNumber].Answer_Values, evaluations["q" & questionNumber & "_other"][CurrentRow]) />
								</cfif>
							</cfif>
						</cfloop>
					<!--- text --->
					<cfelse>
						<cfset survey[questionNumber].Answers[1].Response_Count = IncrementValue(survey[questionNumber].Answers[1].Response_Count) />
						<cfset ArrayAppend(survey[questionNumber].Answers[1].Answer_Values, response) />
					</cfif>
				<!--- did not respond --->
				<cfelse>
					<cfset survey[questionNumber].No_Response_Count = IncrementValue(survey[questionNumber].No_Response_Count) />
					<cfif isSenior>
						<cfset survey[questionNumber].Senior_No_Response_Count = IncrementValue(survey[questionNumber].Senior_No_Response_Count) />
					</cfif>
				</cfif>
			</cfloop>
		</cfloop>
		<!--- calculate percentages --->
		<cfloop index="questionNumber" from="1" to="#ArrayLen(survey)#">
			<cfloop index="answerNumber" from="1" to="#ArrayLen(survey[questionNumber].Answers)#">
				<cfif survey[questionNumber].Answers[answerNumber]["Response_Count"] gt 0 AND survey[questionNumber]["Response_Count"] gt 0>
					<cfset survey[questionNumber].Answers[answerNumber]["Response_Percent"] = DecimalFormat(survey[questionNumber].Answers[answerNumber]["Response_Count"] / survey[questionNumber]["Response_Count"] * 100) />
				<cfelse>
					<cfset survey[questionNumber].Answers[answerNumber]["Response_Percent"] = 0 />
				</cfif>
			</cfloop>
		</cfloop>
		<cfreturn survey />
	</cffunction>
	<!--- format
	<cffunction name="format" output="false" access="public" returntype="any">
		<cfargument name="programYears" required="false" default="" />
		<cfargument name="evaluationIDs" required="false" default="" />
		<cfargument name="studentIDs" required="false" default="" />
		<cfargument name="workshopIDs" required="false" default="" />
		<cfargument name="organizationIDs" required="false" default="" />
		<cfargument name="regionIDs" required="false" default="" />
			<cfscript>
			var evaluations = query(programYears = arguments.programYears, regionIDs = arguments.regionIDs, organizationIDs = arguments.organizationIDs, workshopIDs = arguments.workshopIDs, studentIDs = arguments.studentIDs, evaluationIDs = arguments.evaluationIDs);
			var ret = true;
			/***********************
			Procedures include:
			
			Nullify blank values
			Title case First_Name, Last_Name, Address, City, High_School (Q8), etc.
			Format Phone1 and Phone2 as ###-###-####
			Format DOB as yyyy-mm-dd
			Format Zip_Code as #####
			Format SSN as #### (SSN4)
			Remove illegal characters - (double space), #, /, ?, etc so that only numbers, letters, single spaces, dashes and interior single quotes remain in First_Name, Last_Name, City
		
			var ret = StructNew();
			var allProcs = "capitalize,phone,email,ssn,zipCode,dob,illegalCharacters,exactDuplicates,duplicates";
			var proc = "";
			for (i=1; i lte ListLen(allProcs); i++) {
				proc = ListGetAt(allProcs, i);
				ret[proc] = StructNew();
				ret[proc].formatted = ArrayNew(1);
				ret[proc].invalid = ArrayNew(1);
			}
			ret.recs = evaluations;
			***********************/
		</cfscript>
		<cfloop query="evaluations">
			
		</cfloop>
		<cfreturn ret />
	</cffunction> --->
	<!--- dedupe - exact (hiccup entries) and same person (not hiccups eg different workshops) --->
</cfcomponent>